/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.view.layer;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import soccerscope.model.ClientScene;
import soccerscope.model.Param;
import soccerscope.util.geom.Point2f;
import soccerscope.util.geom.Vector2f;
import soccerscope.view.FieldPane;

public class ControlRegionLayer extends Layer implements ChangeListener {

	public ControlRegionLayer(FieldPane fieldPane, boolean enable) {
		super(fieldPane, enable);
	}

	@Override
	public String getLayerName() {
		return "ControlRegion";
	}

	private JSlider slider;

	private JLabel label;

	@Override
	public JPanel getConfigPanel() {
		JPanel jp = new JPanel();
		this.label = new JLabel("1");
		this.slider = new JSlider(0, 20, 1);
		this.slider.setPaintLabels(true);
		this.slider.setPaintTicks(true);
		this.slider.setPaintTrack(true);
		this.slider.setMajorTickSpacing(5);
		this.slider.addChangeListener(this);
		jp.add(this.label);
		jp.add(this.slider);
		return jp;
	}

	public void stateChanged(ChangeEvent e) {
		this.time = this.slider.getValue();
		this.label.setText(String.valueOf(this.time));
		this.fieldPane.repaint();
	}

	private int time = 1;

	private Point2f drawPoint = new Point2f();

	private Point2f drawSize = new Point2f();

	@Override
	public void draw(Graphics g) {
		if (!this.enable)
			return;

		ClientScene clientScene = this.fieldPane.getScene();
		float playerMagnify = this.fieldPane.getPlayerMagnify();

		for (int i = 0; i < Param.MAX_PLAYER * 2; i++) {
			if (!clientScene.player[i].isEnable()
					|| !this.fieldPane.isSelected(i))
				continue;

			float playerR = clientScene.player[i].getSize() * playerMagnify;
			Color drawColor = clientScene.player[i].getColor();

			float dist = clientScene.player[i].calcDistanceAfterNTime(
					clientScene.player[i].getDashAccelerationMax(), this.time);

			g.setColor(drawColor);
			Point2f ppos = new Point2f(clientScene.player[i].pos);
			ppos.add(Vector2f.polar2vector(dist, clientScene.player[i].angle));
			drawCircle(g, ppos, clientScene.player[i].getKickable());
			drawCircle(g, clientScene.player[i].pos, clientScene.player[i]
					.getKickable());
			drawRect(g, new Point2f(clientScene.player[i].pos.x,
					clientScene.player[i].pos.y
							- clientScene.player[i].getKickable()),
					new Point2f(dist, clientScene.player[i].getKickable() * 2),
					(int) clientScene.player[i].angle,
					clientScene.player[i].pos);

		}
	}
}
